package weaver.interfaces.expense;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.impl.LogFactoryImpl;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.file.LogMan;
import weaver.general.StaticObj;
import weaver.general.Util;
import weaver.interfaces.datasource.DataSource;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.interfaces.util.BigDecimalCalculate;
import weaver.workflow.action.BaseAction;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * Created by Administrator on 2017/6/8.
 * 每周定时同步预算,源码注释联系：345127857@qq.com
 */
public class OASynBudget extends BaseCronJob {
    @Override
    public void execute() {
            String sql = "";
            Log log = LogFactory.getLog(OASynBudget.class);
            RecordSetDataSource resultSet = new RecordSetDataSource("ZJK");
            sql = "select centitycode,centityname,cexnamed,cexcode,cprojectcode,SUM(je)as je from sales_cos where iyear = (DateName(year,GetDate())) group by centitycode,centityname,cexnamed,cexcode,cprojectcode";
            resultSet.execute(sql);

            log.error("==========================同步库房回写数据Start===============================");
            while (resultSet.next()){
                String centitycode = Util.null2String(resultSet.getString("centitycode")).trim();//部门编号
                String sybmmc =  Util.null2String(resultSet.getString("centityname")).trim();//部门名称
                String fymc =  Util.null2String(resultSet.getString("cexnamed")).trim();//费用名称
                String cexcode =  Util.null2String(resultSet.getString("cexcode")).trim();//费用id
                String citemcode =  Util.null2String(resultSet.getString("cprojectcode")).trim();//项目编码
                String bzysje =  Util.null2String(resultSet.getString("je")).trim();//总支出金额
                
                String sqlWhere = " centitycode = '"+centitycode+"' and cexpenseitemcode = '"+cexcode+"' and iyear = (DateName(year,GetDate()))";
                //判断是否有项目编码
                if(!"".equals(citemcode)){
                	//是项目
                	sqlWhere+=" and citemcode = '"+citemcode+"'";
                }
                sql = "select * from EF_EP_V_EPBudget where "+sqlWhere;
                log.error("查询预算的预算信息："+sql);
                RecordSetDataSource rs1 = new RecordSetDataSource("ZJK");
                rs1.execute(sql);

                if (rs1.next()) {
                    //获取当前科室类别总预算
                    String dBBGAmount = rs1.getString("dBBGAmount");
                    RecordSet rss = new RecordSet();
                    //更新OA中的预算台帐总预算
                    sql = "update uf_ystz set dBBGAmount = " + dBBGAmount + " where "+sqlWhere;
                    rss.execute(sql);
                    //更新OA中的预算台帐总已用预算
                    sql = "update uf_ystz set dsumamount = " + bzysje + " where "+sqlWhere;
                    rss.execute(sql);
                    
                    //计算新余额
                    String newDcurrentamount = BigDecimalCalculate.floatSubtract(dBBGAmount, bzysje);
                    //更新OA中的预算台帐余额
                    sql = "update uf_ystz set dcurrentamount = " + newDcurrentamount + " where "+sqlWhere;
                    rss.execute(sql);
                }
            }
            
            log.error("==========================同步库房回写数据End===============================");
    }
}
